//
//  GameViewController.m
//  gong
//
//  Created by echo on 14-1-11.
//  Copyright (c) 2014年 echo. All rights reserved.
//

#import "GameViewController.h"
#import "ScriptParsor.h"
#import "PositionTools.h"
#import "GestureManager.h"
#import "Player.h"
#import "GameManager.h"
#import "UICreator.h"
#import "Constants.h"
#import "PositionTools.h"
#import "TaskManager.h"
#import "StreetView.h"
#import "MemoryView.h"
#import "Constants.h"
#import "TaskManager.h"
#import <QuartzCore/QuartzCore.h>

@interface GameViewController ()

@end

@implementation GameViewController
{
    UIView *_containerView;
    StreetView *_street;
    UIImageView *_cover;
    MemoryView *_memoryView;
    
    UIButton *_oldMemoryBtn;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    [self createCover];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)createCover
{
    _cover = [UICreator createImageViewFromImagename:@"cover.jpg"];
    [self.view addSubview:_cover];
    _cover.center = CGPointMake(self.view.frame.size.height/2, self.view.frame.size.width/2);
    _cover.userInteractionEnabled = YES;    
    
    CGRect buttonFrame = CGRectMake(0, 0, 500, 100);
    UIColor *buttonColor = [UIColor whiteColor];
    UIFont *buttonFont = [UIFont systemFontOfSize:40];
    UIButton *newStoryBtn = [UICreator createButtonWithTitle:@"新的历险"
                                               titleColor:buttonColor
                                                     font:buttonFont
                                                    frame:buttonFrame
                                               buttonType:UIButtonTypeCustom
                                                   target:self action:@selector(initGame)];
    
    [PositionTools placeView:newStoryBtn atTheTopMiddleOfTheView:_cover offset:250];
    [UICreator makeGameButton:newStoryBtn];
    
    if (!_oldMemoryBtn) {
        _oldMemoryBtn = [UICreator createButtonWithTitle:@"旧的回忆"
                                              titleColor:buttonColor
                                                    font:buttonFont
                                                   frame:buttonFrame
                                              buttonType:UIButtonTypeCustom
                                                  target:self action:@selector(loadGame)];
    }

    [self checkCreateOldMemoryButton];
}

- (void)checkCreateOldMemoryButton
{
    [[GameManager sharedInstance] loadMemory];
    
    if ([[GameManager sharedInstance] hasMemory]) {
        [PositionTools placeView:_oldMemoryBtn atTheTopMiddleOfTheView:_cover offset:450];
        [UICreator makeGameButton:_oldMemoryBtn];
        _oldMemoryBtn.hidden = NO;
    } else {
        _oldMemoryBtn.hidden = YES;
    }
}

- (void)initGame
{
    [_street initStreet];
    
    [_memoryView removeFromSuperview];
    _cover.hidden = YES;

    BOOL isInStreet = YES;
    Memory *memory =[GameManager sharedInstance].memory;
    if (memory) {
        isInStreet = memory.isInStreet;
    }
    
    if (_containerView) {
        _containerView.hidden = NO;
        if (!isInStreet) {
            [[NSNotificationCenter defaultCenter] postNotificationName:MsgEnterMemoryHouse object:nil];
        }

    } else {
        _containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.height, self.view.frame.size.width)];
        [self.view addSubview:_containerView];
        _containerView.userInteractionEnabled = YES;
        
        [GameManager sharedInstance].gameMainViewContainer = _containerView;

        [self createStreet];
        [[Player sharedInstance] init:_containerView];
        [self createUI];
        
        if (!isInStreet) {
            [[NSNotificationCenter defaultCenter] postNotificationName:MsgEnterMemoryHouse object:nil];
        } else {
            [[GameManager sharedInstance] gotoStreet];
        }

    }
}

- (void)loadGame
{
    _memoryView = [[MemoryView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.height, self.view.frame.size.width)];
    [PositionTools placeView:_memoryView atTheCenterOfTheView:_cover];
    
    __weak GameViewController *weakSelf = self;
    _memoryView.loadGame = ^(NSInteger saveIndex){
        [[GameManager sharedInstance] loadMemoryByIndex:saveIndex];
        [weakSelf initGame];
    };
}

- (void)createStreet
{
    _street = [[StreetView alloc] initWithFrame:_containerView.frame];
    [PositionTools placeView:_street atTheLeftMiddleOfTheView:_containerView offset:0];
}

- (void)createUI
{
    UIButton *taskButton = [UICreator createButtonWithNormalImage:@"tasknote.png" highlightedImage:nil target:self action:@selector(showTask) use2X:YES];
    [PositionTools placeView:taskButton atTheLeftBottomOfTheView:_containerView offset:CGSizeMake(20, 20)];
    
    UIButton *homeButton = [UICreator createButtonWithNormalImage:@"home.png" highlightedImage:nil target:self action:@selector(gotoCover) use2X:YES];
    [PositionTools placeView:homeButton atTheLeftTopOfTheView:_containerView offset:CGSizeMake(20, 20)];
    
    UIButton *saveButton = [UICreator createButtonWithNormalImage:@"save.png" highlightedImage:nil target:[GameManager sharedInstance] action:@selector(save) use2X:YES];
    [PositionTools placeView:saveButton atTheRightTopOfTheView:_containerView offset:CGSizeMake(20, 20)];
}

- (void)gotoCover
{
    _cover.hidden = NO;
    _containerView.hidden = YES;
    
    [self checkCreateOldMemoryButton];
    
    [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:TaskIndexKey];
    [[TaskManager sharedInstance] clearCurrentTask];
    [[TaskManager sharedInstance] initTaskStatus];

    [[NSNotificationCenter defaultCenter] postNotificationName:MsgRestartGame object:nil];
}

- (void)showTask
{
    NSString *taskDescription = [[TaskManager sharedInstance] getCurrentTask].description;
    [[[UIAlertView alloc] initWithTitle:@"当前任务" message:taskDescription delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show];
}

@end
